package com.github.appintro.internal.viewpager;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.PageSliderProvider;

import java.util.List;

public class PagerAdapter extends PageSliderProvider {
    private List<Component> fragments;

    public PagerAdapter(List<Component> list) {
        this.fragments = list;
    }

    public Object getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    public void setDataa(List<Component> list) {
        this.fragments = list;
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int index) {
        Component component = fragments.get(index);
        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT);
        component.setLayoutConfig(layoutConfig);
        componentContainer.addComponent(component);
        return component;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int index, Object obj) {
        if (componentContainer != null && obj instanceof Component) {
            componentContainer.removeComponent((Component) obj);
        }
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object object) {
        return false;
    }
}
